# encoding: utf-8

#定时任务
# 每日 "预测用户参与表" 中预测结果进行复盘, 更新 "参与者预测准确率积分表" 表

module Capevaluation
  module Business
    module Crontab

      #/data/caiqiu/capevaluation/bin/launch_cmd cmd predict_calculate_stat

      def predict_calculate_stat(params)
        #self.logger.debug(CaiQiu::Support::Utils.get_backtrace_list)
        self.logger.debug(__method__.to_s + " params:#{params}")
        #1.获取需要复盘的赛事
        res_list, match_id_list = Capevaluation::Service::Submits.get_wait_replay_list
        return if match_id_list.size.zero?

        #2.根据玩法和赛果判断是否预测成功，并更新submits表
        res_list.each do |one_res|
          match_id = one_res[:match_id]
          play_code = one_res[:play_code]
          pred_result = one_res[:prediction]
          player = one_res[:player]
          type = one_res[:type]
          if play_code == '001'
            online_result_spf = get_online_wdl_result(match_id)
            next if online_result_spf == ""
            submit_result = one_res[:prediction].to_s
            if submit_result.include?(online_result_spf)
              result_matching = 1
              update_submits(match_id,play_code,pred_result,result_matching)
              update_player_score(type,player,result_matching)
            else
              result_matching = 0
              update_submits(match_id,play_code,pred_result,result_matching)
              update_player_score(type,player,result_matching)
            end
            #elsif play_code == '002'
            #...
          end
        end

        #最后按照新的积分更新排名
        ordered_lis = Capevaluation::Service::PlayerScore.get_ordered_record
        ordered_lis.each_with_index do|one_ordered,index|
          type = one_ordered[:type]
          player = one_ordered[:player]
          ranking = index+1
          Capevaluation::Service::PlayerScore.update_ranking(type,player,ranking)
        end

      end

      #获取线上胜平负赛果
      def get_online_wdl_result(match_id_list)
        result = ""
        rows = Capevaluation::Service::WbaiFootballMatch.get_match_score(match_id_list)
        return result if rows.size == 0
        online_spf_judge(rows)
      end

      #线上胜平负赛果判断
      def online_spf_judge(rows)
        result = ""
        rows.each do |row|
          host_score = row[:host_score].to_i
          away_score = row[:away_score].to_i
          if host_score>away_score
            result = "3"
          elsif host_score==away_score
            result = "1"
          else
            result = "0"
          end
        end
        #puts result
        result
      end

      #更新submits表
      def update_submits(match_id,play_code,pred_result,result_matching)
        Capevaluation::Service::Submits.update_result(match_id,play_code,pred_result,result_matching)
      end

      #更新player_score表
      def update_player_score(type,player,result_matching)
        params = {
            'type' => type,
            'player' => player
        }
        player_scores = Capevaluation::Service::PlayerScore.get_record_by_player(params)

        if player_scores.size > 0
          player_scores.each do|score|
            player_total = score[:total].nil? ? 0:score[:total]
            player_success = score[:success].nil? ? 0:score[:success]
            player_fail = score[:fail].nil? ? 0:score[:fail]
            player_ratio = score[:ratio].nil? ? 0:score[:ratio]
            player_score = score[:score].nil? ? 0:score[:score]
            player_ranking = score[:ranking].nil? ? 0:score[:ranking]
            case result_matching
              when 1 then
                player_total += 1
                player_success += 1
                player_ratio = player_success*100/player_total
                player_score = player_score+10
                Capevaluation::Service::PlayerScore.update_player_score(type,player,player_total,player_success,player_fail,player_ratio,player_score,player_ranking)

              when 0 then
                player_total += 1
                player_fail += 1
                player_ratio = player_success*100/player_total
                Capevaluation::Service::PlayerScore.update_player_score(type,player,player_total,player_success,player_fail,player_ratio,player_score,player_ranking)

            end
          end

        else
          player_total =  0
          player_success =  0
          player_fail = 0
          player_ratio = 0
          player_score = 0
          player_ranking = 0
          case result_matching
            when 1 then
              player_total += 1
              player_success += 1
              player_ratio = player_success*100/player_total
              player_score = player_score+10
              Capevaluation::Service::PlayerScore.add_player_score(type,player,player_total,player_success,player_fail,player_ratio,player_score,player_ranking)

            when 0 then
              player_total += 1
              player_fail += 1
              player_ratio = player_success*100/player_total
              Capevaluation::Service::PlayerScore.add_player_score(type,player,player_total,player_success,player_fail,player_ratio,player_score,player_ranking)

          end
        end

      end

    end
  end
end